package com.yj.config.spring;

import com.mysql.cj.jdbc.Driver;
import com.yj.config.mybatis.pagination.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;

/**
 * Copyright 2017 (C) Yunjian-VC
 * Created on : 2017/11/28
 * Author     : Unbelievable9
 **/
@Configuration
@PropertySource(value = "classpath:mysql-config.properties", encoding = "UTF-8")
@MapperScan("com.yj.*.mapper")
public class JDBCConfig implements EnvironmentAware {

    private Environment environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Profile("dev")
    @Bean(destroyMethod = "close")
    public DataSource devDataSource() {
        DataSource dataSource = new DataSource();
        dataSource.setDriverClassName(Driver.class.getName());
        dataSource.setUrl(environment.getProperty("dev.site.jdbc.url"));
        dataSource.setUsername(environment.getProperty("dev.site.jdbc.username"));
        dataSource.setPassword(environment.getProperty("dev.site.jdbc.password"));
        dataSource.setJmxEnabled(environment.getProperty("dev.site.jdbc.jmxEnabled", Boolean.class));
        dataSource.setTestWhileIdle(environment.getProperty("dev.site.jdbc.testWhileIdle", Boolean.class));
        dataSource.setTestOnBorrow(environment.getProperty("dev.site.jdbc.testOnBorrow", Boolean.class));
        dataSource.setTestOnReturn(environment.getProperty("dev.site.jdbc.testOnReturn", Boolean.class));
        dataSource.setValidationQuery(environment.getProperty("dev.site.jdbc.validationQuery"));
        dataSource.setValidationInterval(environment.getProperty("dev.site.jdbc.validationInterval", Long.class));
        dataSource.setTimeBetweenEvictionRunsMillis(environment.getProperty("dev.site.jdbc.timeBetweenEvictionRunsMillis", Integer.class));
        dataSource.setMaxActive(environment.getProperty("dev.site.jdbc.maxActive", Integer.class));
        dataSource.setInitialSize(environment.getProperty("dev.site.jdbc.initialSize", Integer.class));
        dataSource.setMaxWait(environment.getProperty("dev.site.jdbc.maxWait", Integer.class));
        dataSource.setRemoveAbandonedTimeout(environment.getProperty("dev.site.jdbc.removeAbandonedTimeout", Integer.class));
        dataSource.setMinEvictableIdleTimeMillis(environment.getProperty("dev.site.jdbc.minEvictableIdleTimeMillis", Integer.class));
        dataSource.setMinIdle(environment.getProperty("dev.site.jdbc.minIdle", Integer.class));
        dataSource.setLogAbandoned(environment.getProperty("dev.site.jdbc.logAbandoned", Boolean.class));
        dataSource.setRemoveAbandoned(environment.getProperty("dev.site.jdbc.removeAbandoned", Boolean.class));
        dataSource.setJdbcInterceptors(environment.getProperty("dev.site.jdbc.jdbcInterceptors"));

        return dataSource;
    }

    @Profile("prod")
    @Bean(destroyMethod = "close")
    public DataSource prodDataSource() {
        DataSource dataSource = new DataSource();
        dataSource.setDriverClassName(Driver.class.getName());
        dataSource.setUrl(environment.getProperty("prod.site.jdbc.url"));
        dataSource.setUsername(environment.getProperty("prod.site.jdbc.username"));
        dataSource.setPassword(environment.getProperty("prod.site.jdbc.password"));
        dataSource.setJmxEnabled(environment.getProperty("prod.site.jdbc.jmxEnabled", Boolean.class));
        dataSource.setTestWhileIdle(environment.getProperty("prod.site.jdbc.testWhileIdle", Boolean.class));
        dataSource.setTestOnBorrow(environment.getProperty("prod.site.jdbc.testOnBorrow", Boolean.class));
        dataSource.setTestOnReturn(environment.getProperty("prod.site.jdbc.testOnReturn", Boolean.class));
        dataSource.setValidationQuery(environment.getProperty("prod.site.jdbc.validationQuery"));
        dataSource.setValidationInterval(environment.getProperty("prod.site.jdbc.validationInterval", Long.class));
        dataSource.setTimeBetweenEvictionRunsMillis(environment.getProperty("prod.site.jdbc.timeBetweenEvictionRunsMillis", Integer.class));
        dataSource.setMaxActive(environment.getProperty("prod.site.jdbc.maxActive", Integer.class));
        dataSource.setInitialSize(environment.getProperty("prod.site.jdbc.initialSize", Integer.class));
        dataSource.setMaxWait(environment.getProperty("prod.site.jdbc.maxWait", Integer.class));
        dataSource.setRemoveAbandonedTimeout(environment.getProperty("prod.site.jdbc.removeAbandonedTimeout", Integer.class));
        dataSource.setMinEvictableIdleTimeMillis(environment.getProperty("prod.site.jdbc.minEvictableIdleTimeMillis", Integer.class));
        dataSource.setMinIdle(environment.getProperty("prod.site.jdbc.minIdle", Integer.class));
        dataSource.setLogAbandoned(environment.getProperty("prod.site.jdbc.logAbandoned", Boolean.class));
        dataSource.setRemoveAbandoned(environment.getProperty("prod.site.jdbc.removeAbandoned", Boolean.class));
        dataSource.setJdbcInterceptors(environment.getProperty("prod.site.jdbc.jdbcInterceptors"));

        return dataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis/mybatis-config.xml"));
        sessionFactoryBean.setMapperLocations(new ClassPathResource[]{new ClassPathResource("mybatis/common/PaginationMapper.xml")});
        
        return sessionFactoryBean.getObject();
    }

    @Bean
    PageInterceptor pageInterceptor() {
        return new PageInterceptor();
    }
}
